﻿// -----------------------------------------------------------------------
// <copyright file="Image.cs" company="FCIS">
// You are welcome to do whatever you want with the code in this file
// </copyright>
// -----------------------------------------------------------------------

namespace ImagePackage.Model
{
    /// <summary>
    /// Holds all information related to an image
    /// </summary>
    public class Image
    {
        /// <summary>
        /// One dimension raw pixel values
        /// </summary>
        private byte[] rawPixels;

        /// <summary>
        /// Initializes a new instance of the <see cref="Image"/> class.
        /// </summary>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        public Image(int width, int height)
        {
            this.rawPixels = new byte[width * height * 4];
            this.Width = width;
            this.Height = height;
        }

        /// <summary>
        /// Gets the width.
        /// </summary>
        public int Width { get; private set; }

        /// <summary>
        /// Gets the height.
        /// </summary>
        public int Height { get; private set; }

        /// <summary>
        /// Gets or sets the Raw Pixels.
        /// </summary>
        /// <value>
        /// The pixels.
        /// </value>
        public byte[] RawPixels
        {
            get { return this.rawPixels; }
            set { this.rawPixels = value; }
        }

        /// <summary>
        /// Gets or sets the <see cref="ImagePackage.Model.Pixel"/> with the specified x and y.
        /// </summary>
        /// <param name="x">the row of the pixel</param>
        /// <param name="y">the column of the pixel</param>
        /// <returns>the pixel value at x and y</returns>
        public Pixel this[int x, int y]
        {
            get
            {
                // Bgra 
                int pixelLocation = ((y * this.Width) + x) * 4;

                byte b = this.rawPixels[pixelLocation];
                byte g = this.rawPixels[pixelLocation + 1];
                byte r = this.rawPixels[pixelLocation + 2];
                byte a = this.rawPixels[pixelLocation + 3];
                Pixel p = new Pixel { Blue = b, Green = g, Red = r, Alpha = a };
                return p;
            }

            set
            {
                int pixelLocation = ((y * this.Width) + x) * 4;

                this.rawPixels[pixelLocation] = value.Blue;
                this.rawPixels[pixelLocation + 1] = value.Green;
                this.rawPixels[pixelLocation + 2] = value.Red;
                this.rawPixels[pixelLocation + 3] = value.Alpha;
            }
        }
    }
}
